package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.dubbo.utils.MovementTimeLineService;
import com.tanhua.model.db.Movement;
import com.tanhua.model.db.MovementTimeLine;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

/**
 * @auther : 白月尘
 * @date : 17:15  2021/10/20
 */
@DubboService
public class MovementApiImpl implements MovementApi {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private MovementTimeLineService movementTimeLineService;

    @Override
    public String save(Movement movement) {
        mongoTemplate.save(movement);
        movementTimeLineService.saveTimeLine(movement.getUserId(), movement.getId());
        return movement.getId().toHexString();
    }

    @Override
    public List<Movement> randomMovements(int i) {
        TypedAggregation<Movement> movementTypedAggregation = TypedAggregation.newAggregation(Movement.class, TypedAggregation.sample(i));
        AggregationResults<Movement> results = mongoTemplate.aggregate(movementTypedAggregation, Movement.class);
        return results.getMappedResults();
    }

    @Override
    public List<Movement> findByUserId(Integer page, Integer pageSize, Long userId) {
        Criteria criteria = Criteria.where("userId").is(userId).and("state").is(1);
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("created"))).skip((page - 1) * pageSize).limit(pageSize);
        List<Movement> movements = mongoTemplate.find(query, Movement.class);
        return movements;
    }

    @Override
    public List<Movement> findByFriendId(Integer page, Integer pageSize, long userId) {
        Criteria criteria = Criteria.where("friendId").is(userId).and("state").is(1);
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("created"))).skip((page - 1) * pageSize).limit(pageSize);
        List<MovementTimeLine> movementTimeLines = mongoTemplate.find(query, MovementTimeLine.class);
        List<ObjectId> movementId = CollUtil.getFieldValues(movementTimeLines, "movementId", ObjectId.class);
        Criteria criteria1 = criteria.where("id").in(movementId);
        Query created = Query.query(criteria1).with(Sort.by(Sort.Order.desc("created")));
        List<Movement> movements = mongoTemplate.find(created, Movement.class);
        return movements;
    }

    @Override
    public List<Movement> findByIds(Integer page, Integer pageSize, String[] strings) {
        Criteria criteria = Criteria.where("id").in(strings).and("state").is(1);
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("created"))).limit((page - 1) * pageSize).limit(pageSize);
        return mongoTemplate.find(query, Movement.class);
    }

    @Override
    public Movement findById(String id) {
        return mongoTemplate.findById(new ObjectId(id), Movement.class);
    }

    @Override
    public void updateState(String movementId, int state) {
        Criteria criteria = Criteria.where("id").is(new ObjectId(movementId));
        Query query = Query.query(criteria);
        Update update = Update.update("state", state);
        mongoTemplate.updateFirst(query, update, Movement.class);

    }

    //查询动态
    @Override
    public PageResult findMovements(Integer page, Integer pageSize, Long uid, Integer state) {
        Query query = new Query();
        if (uid != null) {
            query.addCriteria(Criteria.where("userId").is(uid));
        }
        if (state != null) {
            query.addCriteria(Criteria.where("state").is(state));
        }
        long count = mongoTemplate.count(query, Movement.class);
        query.with(Sort.by(Sort.Order.desc("created"))).skip((page-1)*pageSize).limit(pageSize);
        List<Movement> list = mongoTemplate.find(query, Movement.class);
        return new PageResult(page,pageSize,count,list);
    }

    //通过
    public String updateCommentState(String[] message) {
        for (String s : message) {
            ObjectId id = new ObjectId(s);
            Query query = Query.query(Criteria.where("id").is(id));
            Update update = new Update();
            update.set("state",1);
            mongoTemplate.updateFirst(query,update,Movement.class);
        }
        return "修改通过成功";
    }


    //拒绝
    public String updateReState(String[] message) {

        for (String res : message) {
            ObjectId id = new ObjectId(res);
            Query query = Query.query(Criteria.where("id").is(id));
            Update update = new Update();
            update.set("state",2);
            mongoTemplate.updateFirst(query,update,Movement.class);
        }
        return "修改拒绝成功";
    }

}
